<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
           <a-input type="hidden" v-model="model.departId" ></a-input>
          <a-col :span="24">
            <a-form-model-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openingBank">
              <a-input :disabled=formDisabled v-model="model.openingBank" placeholder="请输入开户行"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="银行账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankCardNo">
              <a-input :disabled=formDisabled v-model="model.bankCardNo" placeholder="请输入银行账户"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
  name: 'SysDepartBankInfoForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      flag: false,
      model: {
        bankCardNo: ''
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        openingBank: [
          { required: true, message: '请输入开户行!' },
        ],
        bankCardNo: [
          { required: true, message: '请输入正确的银行卡号!' },
          { pattern: /^[1-9]{1}\d{7,29}$/, message: '请输入正确的银行卡号!' },
        ],
      },
      url: {
        add: '/api/erpmng/sys/sysDepartBankInfo/add',
        edit: '/api/erpmng/sys/sysDepartBankInfo/edit'
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add(depart) {
      this.edit(this.modelDefault);
      this.$set(this.model,"departId",depart);
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true;
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).catch(()=>{

            }).finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  }
}
</script>
